################################################################################
__author__ = 'zhixian'

################################################################################
import cherrypy
import webpages

################################################################################
@cherrypy.expose
def index():
    """Handler for HTTP-GET to /login"""
    ctx = {
        '__name__': __name__
    }
    # TODO: Add another logic here that says that if user is already logged in, redirect user to root page
    #raise cherrypy.HTTPRedirect(url)
    # TODO: Add another logic here that says that if user is already logged in, clear session credentials
    #raise cherrypy.HTTPRedirect(url)

    return webpages.page_output(__name__, None)

@cherrypy.expose
def authenticate(username, password):
    """Handler for HTTP-POST to /login/authenticate
    Status: TODO
    """

    is_authenticated = True

    if is_authenticated:
        import tools

        cherrypy.session[tools.SESSION_USERNAME] = username
        x = cherrypy.session.get('SESSION_USERNAME  ')
        cherrypy.lib.sessions.save()
        url = cherrypy.session.get(tools.BEFORE_LOGIN_URL)
        if url is not None and url.lower()=='/logout':
            url = None

        if url is None:
            url = '/'

        raise cherrypy.HTTPRedirect(webpages.PageHelper.get_url(url))
        return "Entered {0} with password {1}".format(username, password)
    else:
        return "is_authenticated failed. Please check your credentials."
